import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)



//引入子模块
import menu from './modules/menu'
import role from './modules/role'
import member from './modules/member'
import cate from './modules/cate'
import specs from './modules/specs'
import goods from './modules/goods'
import user1 from './modules/user'  //有bug
import banner from './modules/banner'
import seckill from './modules/seckill'
import home from './modules/home'


export default new Vuex.Store({
  state: {
    user: sessionStorage.getItem('user') ? JSON.parse(sessionStorage.getItem('user')) : null
  },
  getters: {
    user(state) {
      return state.user
    }
  },
  mutations: {
    changeUser(state, user) {
      if (user) {
        //修改状态管理
        state.user = user
        //数据缓存
        sessionStorage.setItem('user', JSON.stringify(user))
      } else {
        sessionStorage.removeItem('user')
      }
    }
  },
  actions: {
    userActions(context, user) {
      context.commit('changeUser', user)
    }
  },
  modules: {
    menu,  //菜单列表
    role,  //角色
    member,  //管理员
    cate,  //商品分类
    specs,  //商品规格
    goods,  //商品
    user1,  //会员中心
    banner,  //轮播图
    seckill,  //秒杀活动
    home

  }
})
